package com.bread.tliaswebmanagement.exception;

import com.bread.tliaswebmanagement.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result handleException(Exception e){
        log.error("服务器发生异常: {}", e);
        return Result.error("服务器发生异常");
    }

    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException e){
        log.error("服务器发生异常: {}, {}", e.getClass(), e.getMessage());
        String msg = e.getMessage();
        String errMsg = msg.substring(msg.indexOf("Duplicate entry"));
        String[] s = errMsg.split(" ");
        return Result.error(s[2]+"已存在");
    }

    @ExceptionHandler
    public Result handleDeptDeleteFailException(DeptDeleteFailException e){
        log.error("服务器发生异常: {}, {}", e.getClass(), e.getMessage());
        return Result.error(e.getMessage());
    }

}
